const db = wx.cloud.database();
const _= db.command;
var app=getApp();
 var y=app.globalData.y;
wx-Page({

    data: {
        container: null,
        value: '',
        landList:[],
        toolList:[]
    },

    onLoad: function(options) {

        this.getLand();
        this.getTool();
        wx.cloud.callFunction({
            name: 'getUser',
            }).then(res => {
                wx.setStorageSync('userFlag', res.result);
            }).catch(console.error)

            var user = wx.getStorageSync('userFlag')
            
            if(user.flag == 1){
                db.collection("chatlist").where({
                    _id:db.RegExp({
                        regexp:'^.*'+user.user._id,
                    })
                }).get().then(res=>{
                    var y=0;
                    var friendes = res.data;
                    var i=0;
                    for(;i<friendes.length;i++)
                    {
                      var o = friendes[i].chat.length;
                      if(o>0&&friendes[i].chat[o-1].sendid!=user._id&&!friendes[i].chat[o-1].state)
                      {
                          y++;
                      }
                    }
                    if(y>0)
                    {
                        wx.showTabBarRedDot({
                          index: 2,
                        })
                    }
                })
            }
            
    },
    getLand(){
        var that = this;
        wx.cloud.callFunction({
            // 云函数名称
            name: 'getLand',
            // 传给云函数的参数
            data:{
                num:10,
                page:0,
                label:'',
                type:'',
                area:''
            }
        }).then(res => {
            that.setData({
                landList : res.result.data  
            })
        }).catch(console.error)
    },
    getTool(){
        var that = this;
        wx.cloud.callFunction({
            // 云函数名称
            name: 'getTool',
            // 传给云函数的参数
            data:{
                num:10,
                page:0,
                type:""
            }
        }).then(res => {
            that.setData({
                toolList : res.result.data  
            })
        }).catch(console.error)
    },
    onReady: function() {
        //Do some when page ready.
        this.setData({
        container: () => wx.createSelectorQuery().select('#container'),
        });
    },
    onshow()
    {
        if(y>0)
        {
            wx.showTabBarRedDot({
              index: 2,
            })
        }
        else{
            wx.hideTabBarRedDot({
              index: 2,
            })
        }
    },
    onPullDownRefresh:function () {
        setTimeout(()=>{ wx.stopPullDownRefresh({
        success: (res) => {}})},1000)  
    }
})
